# %%
import requests
from urllib.parse import urlparse, parse_qs
import os


# %%

get_url = "https://wjyxoms.centuryenglish.com/sharePage/book-detail.html?bookId=485&userId=1193440"

post_url = "https://wjyxresource.centuryenglish.com/api/book/getInfo"

# %%

query_params = parse_qs(urlparse(get_url).query)
print(query_params)

# %%
book_id = query_params["bookId"][0]
user_id = query_params["userId"][0]
print(book_id, user_id)

# %%
data = {
    "bookId": book_id,
    "userId": user_id,
}
res = requests.post(post_url, json=data)

# %%
print(res.status_code)

# %%

data = res.json()["data"]

print(data)

# %%

dir_name = data["bookName"]

if not os.path.exists(dir_name):
    os.mkdir(dir_name)

# %%

for r in data["records"]:
    url = r["recordUrl"]
    name = r["recordName"]
    res = requests.get(url)
    mp3_path = os.path.join(dir_name, f"{name}.map3")
    with open(mp3_path, "wb") as f:
        f.write(res.content)
    print(f"download {name} success")
